<html>
<head><meta charset="utf-8"><title>FQ name from attribute macro · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/FQ.20name.20from.20attribute.20macro.html">FQ name from attribute macro</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="232183491"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/FQ%20name%20from%20attribute%20macro/near/232183491" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Diggsey <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/FQ.20name.20from.20attribute.20macro.html#232183491">(Mar 28 2021 at 18:20)</a>:</h4>
<p>Is it possible to get the fully-qualified name of an item to which an attribute macro is applied?</p>



<a name="232187260"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/FQ%20name%20from%20attribute%20macro/near/232187260" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/FQ.20name.20from.20attribute.20macro.html#232187260">(Mar 28 2021 at 19:23)</a>:</h4>
<p>A proc-macro cannot do that; we would, at the very least, need something like an unstringified <code>module_path!()</code>,</p>
<ul>
<li>note that if you just want the stringified FQ name for debugging purposes or some other reason, then <code>concat!(module_path!(), "::", stringify!($item_name))</code> works Just Fine™.</li>
</ul>
<p>which would thus need arbitrary token expansion which is achieved by using callback-style (otherwise we'd end up with the same limitations as <code>concat_idents!</code>). This means that, similar to <code>concat_idents!</code> being fixable if instead it were <code>with_concat_idents!</code>:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="n">with_concat_idents</span><span class="o">!</span><span class="p">(</span><span class="n">foo</span><span class="p">,</span><span class="w"> </span><span class="n">_</span><span class="p">,</span><span class="w"> </span><span class="n">bar</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="cp">$fname</span>:<span class="nc">ident</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">(</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="cp">$fname</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="p">));</span><span class="w"></span>
</code></pre></div>
<p>an unstringified <code>module_path!</code> would be possible if it were:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="n">with_module_path</span><span class="o">!</span><span class="w"> </span><span class="p">{(</span><span class="w"> </span><span class="cp">$($path</span>:<span class="nc">tt</span><span class="p">)</span><span class="o">*</span><span class="w"> </span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">(</span><span class="w"></span>
<span class="w">    </span><span class="c1">// …</span>
<span class="p">)}</span><span class="w"></span>
</code></pre></div>
<p>That would still not always be enough, since the item to which an attribute macro is applied could still be an associated item within an <code>impl</code> block, or a <code>trait</code> definition.</p>
<hr>
<p>From all this, maybe there is a way to circumvent this issue and get to whatever it is you want to achieve here (XY yadda yadda), if you describe your ulterior objective <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span></p>



<a name="232188379"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/FQ%20name%20from%20attribute%20macro/near/232188379" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Diggsey <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/FQ.20name.20from.20attribute.20macro.html#232188379">(Mar 28 2021 at 19:44)</a>:</h4>
<p><code>module_path!</code> should work fine for my use-case, thanks :)</p>



<a name="232188421"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/FQ%20name%20from%20attribute%20macro/near/232188421" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Diggsey <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/FQ.20name.20from.20attribute.20macro.html#232188421">(Mar 28 2021 at 19:45)</a>:</h4>
<p>I'm writing a task queue and wanted a way to generate a default name for tasks</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>